package cn.wizzer.app.web.modules.controllers.cms;

import cn.wizzer.app.cms.modules.services.CmsArticleService;
import cn.wizzer.app.cms.modules.services.CmsChannelService;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

@At("/cms/article")
@IocBean
public class ApiArticleController {
    @Reference
    @Inject
    CmsArticleService articleService;
    @Reference
    @Inject
    CmsChannelService channelService;

    @At("/category")
    @Ok("json:full")
    public Result category() {
        return Result.success(channelService.query(Cnd.where("disabled", "=", 0).desc("opAt")));
    }

    @At("/get")
    @Ok("json:full")
    public Result get(@ParamVerify(required = true) int pageNumber,
                      @ParamVerify(required = true) int pageSize,
                      String cateId) {
        Cnd where = Cnd.where("disabled", "=", 0);
        where.and(new Static(System.currentTimeMillis() / 1000 + " between publishAt and endAt"));
        if (Strings.isNotBlank(cateId)) {
            where.and("channelId", "=", cateId);
        }
        return Result.success(articleService.getListPage(pageNumber, pageSize, where.desc("opAt")));
    }

    @At("/detail")
    @Ok("json:full")
    public Result detail(@ParamVerify(name = "id", required = true) String id) {
        Cnd where = Cnd.where("id", "=", id);
        return Result.success(articleService.getArticle(where));
    }
}
